package com.project.m.api.busi.handlers.hicatcard.Inner;

import com.project.m.api.busi.bean.inner.InnerUserTransactionQueryReq;
import com.project.m.api.busi.bean.inner.InnerUserTransactionQueryResp;
import com.project.m.api.busi.config.BusiError;
import com.project.m.api.busi.db.entity.hicatcard.user.TUser;
import com.project.m.api.busi.db.service.hicatcard.transaction.TransactionService;
import com.project.m.api.busi.db.service.hicatcard.user.UserService;
import com.project.m.api.busi.handlers.MerchantBizParamChecker;
import com.project.m.api.validates.ValidateParam;
import com.project.m.api.validates.ValidateRet;
import com.project.m.api.validates.ValidateType;
import com.project.m.api.validates.ValidateUtil;
import com.project.m.api.busi.bean.hicatcard.TransactionItem;
import com.project.m.api.busi.util.TimeUtil;
import com.project.m.api.common.biz.BizHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Component
public class InnerUserTransactionQueryBizHandler extends MerchantBizParamChecker<InnerUserTransactionQueryReq, InnerUserTransactionQueryResp>
        implements BizHandler<InnerUserTransactionQueryReq, InnerUserTransactionQueryResp> {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    UserService userService;

    @Autowired
    TransactionService transactionService;


    @Override
    public InnerUserTransactionQueryResp handle(InnerUserTransactionQueryReq bizRequest) throws Exception {
        InnerUserTransactionQueryResp bizResp = new InnerUserTransactionQueryResp();

        try {
            ValidateRet vRet = checkParam(bizRequest, bizResp);
            if (!vRet.isValid()) {
                return bizResp;
            }

            List<TUser> userList = userService.findByPhoneAndEnable(bizRequest.getPhone(), true);
            if (CollectionUtils.isEmpty(userList)) {
                bizResp.setResult_code(BusiError.ERR_CODE_USER_NONE);
                bizResp.setError_message(BusiError.ERR_MSG_USER_NONE);
                return bizResp;
            }

            TUser user = userList.get(0);

            // 根据 source/ status 来取数据
            Sort.Order order = new Sort.Order(Sort.Direction.DESC, "createTime");
            Sort sort = new Sort(order);
            Pageable pageable = new PageRequest(bizRequest.getPage() - 1, bizRequest.getPagesize(), sort);

            // 取数据
            List<TransactionItem> transactionItemList = transactionService.findTransactionItemByUserIdAndMerchantId(
                    user.getUserId(), null, "", "", "", "", pageable.getPageNumber(),
                    pageable.getPageSize(), pageable);

            for (TransactionItem transaction : transactionItemList) {
                transaction.setCreate_time(TimeUtil.transferDateFormat(transaction.getCreate_time(),
                        TimeUtil.DATE_PATTERN_NOSEPARTOR, "yyyy/MM/dd HH:mm"));
                transaction.setPhone(user.getPhone());
            }

            bizResp.setTransactions(transactionItemList);


            bizResp.setResult_code(BusiError.ERR_CODE_SUCCESS);
            bizResp.setError_message("");
            return bizResp;
        } catch (Exception e) {
            logger.error("InnerUserRegisterBizHandler", e);
            bizResp.setResult_code(BusiError.ERR_CODE_DB_SAVE);
            bizResp.setError_message(BusiError.ERR_MSG_DB_SAVE);
            return bizResp;
        }
    }

    @Override
    public ValidateRet checkParam(InnerUserTransactionQueryReq req, InnerUserTransactionQueryResp resp) {
        ValidateRet vRet = super.checkParam(req, resp);
        if (!vRet.isValid())
            return vRet;
        List<ValidateParam> paramList = new ArrayList<ValidateParam>();
        paramList.add(new ValidateParam("phone", ValidateType.NOTBLANK, req.getPhone()));
        paramList.add(new ValidateParam("page", ValidateType.NOTBLANK, req.getPage()));
        paramList.add(new ValidateParam("pagesize", ValidateType.NOTBLANK, req.getPagesize()));

        vRet = ValidateUtil.validate(paramList);
        if (vRet == null) {
            vRet = new ValidateRet(true, "");
        }

        if (!vRet.isValid()) {
            resp.setResult_code(BusiError.ERR_CODE_PARAM_BAD_REQ);
            resp.setError_message(vRet.getErrMsg());
        }

        return vRet;
    }


}
